/*
 author:王立雍
 date:2014-08-14 19:07:00
 desc:jquery扩展分页方法
 */
(function($){
    $.fn.pageLation = function(os){
        var options = {//默认分页参数设置
            container: '#yirenzheng',//数据表格div容器
            url: '',//发送请求的地址
            success: function(msg,o){},//成功后执行的回调函数
            currentPage: 1,
            pageForm:null,
            pageSize: 10,
            param: {},//附加参数
            type: "post",//可选：action,
            isEvent:false
        };
        var o = $.extend(options, os);
        $.page.loadData(o,function(flag){
            $.paging(flag,o);
        });
    }
    /**
     * @classDescription 执行click模式下的逻辑代码
     * @param {Object} o
     */
    $.page = {
        loadData: function(o,callback){
            var that = this;
            var data = that.genData(o);
            $.ajax({
                url: o.url,
                type:o.type,
                data: data,
                cache: false,
                success: function(result){
                    var rs;
                    try{
                        rs = JSON.parse(result);
                    }catch(e){
                        rs = eval(result);
                    }
                    if($(o.container).find("table").hasClass("table-col")){
                        $(o.container).find("table tr:first-child").next().nextAll().remove();
                    }else{
                        $(o.container).find("table tr:first-child").nextAll().remove();
                    }
                    if((rs) && (rs.dataList) && (rs.dataList.length) && (rs.dataList != "null") && (rs.dataList != null)){
                        callback(true);
                        o.success(rs.dataList,o);
                        o.totalPage = rs.pageCount;
                        o.totalCount = rs.totalCount;
                        var pageObj = that.findPage(o);
                        pageObj.countPageObj.html(o.totalPage);
                        pageObj.totalCount.html(o.totalCount);

                    }else{
                        callback(false);
                    }
                },
                error: function(){console.log("error");}
            });
        },
        findPage:function(o){//获取分页对象
            var paging = {
                prevObj:$(o.container).parent().next(".paging").find(".page-up"),//上一页对象
                nextObj:$(o.container).parent().next(".paging").find(".page-down"),//下一页对象
                pageNoObj:$(o.container).parent().next(".paging").find(".first-num"),//页码对象
                countPageObj:$(o.container).parent().next(".paging").find(".first-num").next("span"),//总页数对象
                pageInputObj:$(o.container).parent().next(".paging").find(".page-input"),//页码输入框对象
                pageButtonObj:$(o.container).parent().next(".paging").find(".page-input").next("button"),//按钮对象
                totalCount:$(o.container).parent().next(".paging").find(".total-count")//总条数对象
            }
            return paging;
        },
        genData: function(o){
            var pdata = $.extend({}, {
                "currentPage": o.currentPage,
                "pageSize": o.pageSize
            }, o.param);
            var sdata = $.extend({}, pdata, $.jsonObj(o.pageForm));
            return sdata;
        },
        handler: function(o){
            var that = this;
            var pageObj = that.findPage(o);
            pageObj.prevObj.on('click', function(){
                var p = parseInt(pageObj.pageNoObj.html());
                o.currentPage = p - 1;
                if (o.currentPage <= 0) {
                    o.currentPage = 1;
                }else{
                    pageObj.pageNoObj.html(o.currentPage);
                    that.loadData(o,function(flag){});
                }

            });

            pageObj.nextObj.on('click', function(){
                var p = parseInt(pageObj.pageNoObj.html());
                o.currentPage = p + 1;
                if (o.currentPage > o.totalPage) {
                    o.currentPage = o.totalPage;
                }else{
                    pageObj.pageNoObj.html(o.currentPage);
                    that.loadData(o,function(){});
                }

            });
            pageObj.pageButtonObj.on('click', function(){
                var numMatchs = /^[0-9]{1,}$/;
                if((!numMatchs.test(pageObj.pageInputObj.val())) || parseInt(pageObj.pageInputObj.val()) <= 0 || parseInt(pageObj.pageInputObj.val()) > o.totalPage){
                    pageObj.pageInputObj.val("");
                }else{
                    var p = parseInt(pageObj.pageInputObj.val());
                    o.currentPage = p;
                    pageObj.pageNoObj.html(o.currentPage);
                    that.loadData(o,function(){});
                }

            });
            pageObj.pageInputObj.keyup(function(e){
                var e = e || window.event;
                if (e.keyCode == '13' || e.keyCode == 13) {
                    var numMatchs = /^[0-9]{1,}$/;
                    if((!numMatchs.test(pageObj.pageInputObj.val())) || parseInt(pageObj.pageInputObj.val()) <= 0 || parseInt(pageObj.pageInputObj.val()) > o.totalPage){
                        pageObj.pageInputObj.val("");
                    }else{
                        var p = parseInt(pageObj.pageInputObj.val());
                        o.currentPage = p;
                        pageObj.pageNoObj.html(o.currentPage);
                        that.loadData(o,function(){});
                    }
                }
            });
            if(o.isEvent){
                return;
            }
            $(o.pageForm).keyup(function(e){
                if (e.keyCode == '13' || e.keyCode == 13) {
                    o.currentPage = 1;
                    that.loadData(o,function(flag){
                        $.paging(flag,o);
                    });
                }
            });
            $(o.pageForm).find(".dropdown-list").find("ul").find("li").on("click",function(){
                o.currentPage = 1;
                that.loadData(o,function(flag){
                    $.paging(flag,o);
                });
            });

            $(o.pageForm).submit(function(){return false});
        }
    }

    $.jsonObj = function(form){
        //判断是否有序列化的东东
        if (!$(form).html() || $(form).html() == null || $.trim($(form).html()) == "") {
            return null;
        }
        var formEl = $(form).find('input[type="text"],input[type="hidden"]');
        var json = "{";
        for (var i = 0; i < formEl.length - 1; i++) {
            var name = formEl.eq(i).attr('name');
            var val = "'" + formEl.eq(i).val() + "'";
            json += name;
            json += ":";
            json += val;
            json += ",";
        }
        var lname = formEl.eq(formEl.length - 1).attr('name');
        var lval = "'" + formEl.eq(formEl.length - 1).val() + "'";
        json += lname;
        json += ":";
        json += lval;
        json += "}";
        var jsonObj = eval("(" + json + ")")
        return jsonObj;
    }
    $.appendData = function(str,o){
        $(o.container).find("table").append(str);
    }
    $.paging = function(flag,o){
    	 $(".paging").remove();
         $(".no-data").remove();
         $(".paging").die();
        if(flag){
            var pages = '<div class="paging"><span class="total-label">总计:</span><b class="total-count"></b><a class="page-up" href="javascript:void(0);">上一页</a><span class="first-num">1</span>/<span>6</span><a class="page-down" href="javascript:void(0);">下一页</a><input type="text" class="page-input"/><button>跳转</button></div>';
            $(o.container).parent().after(pages);
            $.page.handler(o);
            o.isEvent = true;
        }else{
            $(o.container).find("table").css({borderBottom:"none"});
            var noData = $('<div class="no-data" style="text-align:center;width: 100%;height:400px;background-color: #fff;border-bottom:1px solid #EAEDEE;border-right:1px solid #EAEDEE;border-left:1px solid #EAEDEE;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;"><img style="margin:110px 0 35px 0" src="/new/images/undefined-data.png" /><p style="color:#bec4c4">暂时还没有数据!</p></div>');
            $(o.container).append(noData);
        }
    }
})(jQuery);